package com.graduation.cdm.funactivity.view.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.graduation.cdm.funactivity.R;
import com.graduation.cdm.funactivity.bean.User;
import com.graduation.cdm.funactivity.view.LoginActivity;
import com.graduation.cdm.funactivity.view.mineview.CollectActivity;
import com.graduation.cdm.funactivity.view.mineview.EvaluateActivity;
import com.graduation.cdm.funactivity.view.mineview.SqliteActivity;
import com.graduation.cdm.funactivity.view.mineview.UserInfoActivity;

import static android.content.Context.MODE_PRIVATE;
import static com.graduation.cdm.funactivity.util.GlobalConstant.preUrl;
import static com.graduation.cdm.funactivity.util.xUtilsImageUtils.displayHead;

/**
 * Created by cdm on 2017/3/1.
 */

public class MineFragment extends Fragment implements View.OnClickListener {

    private final int USERINFO = 1;

    private View view;
    User user;

    private ImageView ivHead;
    private TextView tvUserName;
    private Button btnShouCang;
    private Button btnPingJia;
    private RelativeLayout rlUser;
    private RelativeLayout rlDraft;
    private RelativeLayout rlExit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine,null);

        initView();
        getUser();

        return view;
    }

    private void getUser(){
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user_info", MODE_PRIVATE);
        Integer userId = sharedPreferences.getInt("userId",0);
        String userName = sharedPreferences.getString("userName",null);
        String userPassword = sharedPreferences.getString("userPassword",null);
        String userSex = sharedPreferences.getString("userSex",null);
        String userPhone = sharedPreferences.getString("userPhone",null);
        String userEmail = sharedPreferences.getString("userEmail",null);
        String imgUrl = sharedPreferences.getString("imgUrl",null);
        user = new User(userId,userSex,userName,userPassword,userPhone,userEmail,imgUrl);

        displayHead(ivHead,preUrl+imgUrl);
        tvUserName.setText(user.getUserName());
    }

    private void initView(){
        ivHead = ((ImageView) view.findViewById(R.id.iv_head));
        tvUserName = ((TextView) view.findViewById(R.id.tv_user_name));
        btnShouCang = ((Button) view.findViewById(R.id.btn_shoucang));
        btnPingJia = ((Button) view.findViewById(R.id.btn_pingjia));
        rlUser = ((RelativeLayout) view.findViewById(R.id.rl_user));
        rlDraft = ((RelativeLayout) view.findViewById(R.id.rl_draft));
        rlExit = ((RelativeLayout) view.findViewById(R.id.rl_exit));

        ivHead.setOnClickListener(this);
        btnShouCang.setOnClickListener(this);
        btnPingJia.setOnClickListener(this);
        rlUser.setOnClickListener(this);
        rlExit.setOnClickListener(this);
        rlDraft.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_head:
                break;
            case R.id.btn_shoucang:
                Intent intent_collect = new Intent(getActivity(), CollectActivity.class);
                startActivity(intent_collect);
                break;
            case  R.id.btn_pingjia:
                Intent intent_evaluate = new Intent(getActivity(), EvaluateActivity.class);
                startActivity(intent_evaluate);
                break;
            case R.id.rl_user:
                Intent intent_user = new Intent(getActivity(), UserInfoActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("userInfo",user);
                intent_user.putExtras(bundle);
                startActivityForResult(intent_user,USERINFO);
                break;
            case R.id.rl_draft:
                Intent intent_draft = new Intent(getActivity(), SqliteActivity.class);
                startActivity(intent_draft);
                break;
            case R.id.rl_exit:
                Intent intent_exit = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent_exit);
                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user_info",MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean("isLogin",false);
                editor.commit();
                getActivity().finish();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == USERINFO){
            System.out.println("sssssssssss");
            getUser();
        }
    }
}
